﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace HorrorsOfArkham.Game_Application_Layer
{
    public class Logger
    {
        #region Members

        public static readonly Logger Instance = new Logger();

        private string logFileDirectory;

        public string LogFileDirectory
        {
            get
            { return logFileDirectory; }
        }

        private string currentLogFile;

        public string CurrentLogFile
        {
            get
            { return currentLogFile; }
        }

        #endregion

        #region Initialization

        protected Logger()
        {
        }

        public bool SetLogFileDirectory()
        {
            bool setupOK = false;

            setupOK = checkDirectory();

            setupOK = checkLogFile();

            return setupOK;
        }

        private bool checkDirectory()
        {
            bool setupOK = false;

            string currentDir = Directory.GetCurrentDirectory();

            string newLogDirectory = currentDir + "\\Logs\\" + DateTime.Today.ToShortDateString();

            if (!Directory.Exists(@newLogDirectory))
            {
                Directory.CreateDirectory(newLogDirectory);

                if (Directory.Exists(@newLogDirectory))
                {
                    logFileDirectory = newLogDirectory;
                    setupOK = true;
                }
                else
                {
                    setupOK = false;
                }

            }
            else
            {
                logFileDirectory = newLogDirectory;
                setupOK = true;
            }

            return setupOK;
        }

        private bool checkLogFile()
        {
            bool setupOK = false;

            string currentLogFileName = DateTime.Now.ToString() + ".txt";

            for (int i = 0; i < currentLogFileName.Length; i++ )
            {
                //Console.WriteLine("löytyi: " + currentLogFileName.ElementAt(i));
                if (currentLogFileName.ElementAt(i).ToString().Equals(":"))
                {
                    Console.WriteLine("löytyi: " + currentLogFileName.ElementAt(i));

                    currentLogFileName = currentLogFileName.Remove(i, 1);
                }
            }
            Console.WriteLine(currentLogFileName);

            if (Directory.Exists(logFileDirectory))
            {
                if (Directory.EnumerateFiles(logFileDirectory, currentLogFileName).Count() > 0)
                {
                    setupOK = false;
                }
                else 
                {
                    currentLogFile = currentLogFileName;

                    setupOK = true;
                }
            }
            else
            {
                setupOK = false;
            }

            return setupOK;
        }

        #endregion

        #region Console Debugging

        public void DebugLine(Object line)
        {
            Console.WriteLine(line.ToString());

            LogLine(line);
        }

        #endregion

        #region Logging

        public void LogLine(Object line)
        {
            StringBuilder sb = new StringBuilder();

            sb.AppendLine(line.ToString());

            using (StreamWriter outfile = new StreamWriter(logFileDirectory + @"\TestLogFile.txt"))
            {
                outfile.Write(sb.ToString());
            }
        }

        #endregion
    }
}
